/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       town_drawing.cpp

	$Header: /game/town_drawing.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "town_drawing.h"

#include "elements_of.h"

namespace 
{
	// --------------------------------------------------------
	// draw order for various towns
	// --------------------------------------------------------
	static t_town_building const k_life_drawing_order[] = 
	{
		k_town_village_hall,
		k_town_hall,
		k_town_city_hall,
		k_town_grail,
		k_town_fort,
		k_town_citadel,
		k_town_castle,
		k_town_caravan,
		k_town_prison,
		k_town_dwelling_pikeman,
		k_town_dwelling_ballista,
		k_town_dwelling_crusader,
		k_town_dwelling_monk,
		k_town_dwelling_champion,
		k_town_dwelling_angel,
		k_town_blacksmith,
		k_town_stables,
		k_town_abbey,
		k_town_seminary,
		k_town_mage_guild_1,
		k_town_mage_guild_2,
		k_town_mage_guild_3,
		k_town_mage_guild_4,
		k_town_mage_guild_5,
		k_town_library_1,
		k_town_library_2,
		k_town_dwelling_squire,
		k_town_dwelling_crossbowman,
		k_town_shipyard,
		k_town_tavern,
	};

	static t_town_building const k_order_drawing_order[] = 
	{
		k_town_fort,
		k_town_citadel,
		k_town_castle,
		k_town_village_hall,
		k_town_hall,
		k_town_city_hall,
		k_town_grail,
		k_town_prison,
		k_town_caravan,
		k_town_dwelling_dragon_golem,
		k_town_dwelling_titan,
		k_town_dwelling_naga,
		k_town_dwelling_genie,
		k_town_dwelling_golem,
		k_town_dwelling_mage,
		k_town_treasury,
		k_town_blacksmith,
		k_town_mage_guild_1,
		k_town_mage_guild_2,
		k_town_mage_guild_3,
		k_town_mage_guild_4,
		k_town_mage_guild_5,
		k_town_library_1,
		k_town_library_2,
		k_town_tavern,
		k_town_university,
		k_town_dwelling_dwarf,
		k_town_dwelling_halfling,
		k_town_shipyard,
	};

	static t_town_building const k_death_drawing_order[] = 
	{
		k_town_village_hall,
		k_town_hall,
		k_town_city_hall,
		k_town_fort,
		k_town_citadel,
		k_town_castle,
		k_town_caravan,
		k_town_prison,
		k_town_grail,
		k_town_dwelling_ghost,
		k_town_dwelling_cerberus,
		k_town_dwelling_vampire,
		k_town_dwelling_venom_spawn,
		k_town_dwelling_bone_dragon,
		k_town_dwelling_devil,
		k_town_blacksmith,
		k_town_mage_guild_1,
		k_town_mage_guild_2,
		k_town_mage_guild_3,
		k_town_mage_guild_4,
		k_town_mage_guild_5,
		k_town_library_1,
		k_town_library_2,
		k_town_dwelling_skeleton,
		k_town_dwelling_imp,
		k_town_skeleton_transformer,
		k_town_necromancy_amplifier,
		k_town_tavern,
		k_town_shipyard,
	};

	static t_town_building const k_chaos_drawing_order[] = 
	{
		k_town_grail,
		k_town_village_hall,
		k_town_hall,
		k_town_city_hall,
		k_town_fort,
		k_town_citadel,
		k_town_castle,
		k_town_dwelling_medusa,
		k_town_dwelling_minotaur,
		k_town_dwelling_nightmare,
		k_town_dwelling_efreet,
		k_town_dwelling_hydra,
		k_town_dwelling_black_dragon,
		k_town_caravan,
		k_town_prison,
		k_town_dwelling_centaur,
		k_town_training_grounds,
		k_town_blacksmith,
		k_town_library_2,
		k_town_mage_guild_1,
		k_town_mage_guild_2,
		k_town_mage_guild_3,
		k_town_mage_guild_4,
		k_town_mage_guild_5,
		k_town_library_1,
		k_town_academy,
		k_town_dwelling_berserker,	
		k_town_tavern,
		k_town_shipyard,
		k_town_mana_vortex,
	};

	static t_town_building const k_nature_drawing_order[] = 
	{
		k_town_village_hall,
		k_town_hall,
		k_town_city_hall,
		k_town_fort,
		k_town_citadel,
		k_town_castle,
		k_town_shipyard,
		k_town_caravan,
		k_town_prison,
		k_town_blacksmith,
		k_town_grail,
		k_town_rainbow,
		k_town_dwelling_unicorn,
		k_town_dwelling_griffin,
		k_town_dwelling_fairie_dragon,
		k_town_dwelling_pheonix,
		k_town_dwelling_elf,			
		k_town_dwelling_tiger,			
		k_town_mage_guild_1,
		k_town_mage_guild_2,
		k_town_mage_guild_3,
		k_town_mage_guild_4,
		k_town_mage_guild_5,
		k_town_library_1,
		k_town_library_2,
		k_town_grove,
		k_town_dwelling_wolf,		
		k_town_dwelling_fairie,			
		k_town_tavern,
		k_town_portal,
	};

	static t_town_building const k_might_drawing_order[] = 
	{
		k_town_grail,
		k_town_village_hall,
		k_town_hall,
		k_town_city_hall,
		k_town_fort,
		k_town_citadel,
		k_town_castle,
		k_town_shipyard,
		k_town_caravan,
		k_town_prison,
		k_town_dwelling_thunderbird,
		k_town_dwelling_behemoth,
		k_town_dwelling_ogre,
		k_town_dwelling_cyclops,
		k_town_dwelling_nomad,
		k_town_dwelling_harpy,
		k_town_breeding_pit,
		k_town_training_grounds,
		k_town_arena,
		k_town_dwelling_berserker,	
		k_town_dwelling_centaur,
		k_town_blacksmith,
		k_town_magic_dampener,
		k_town_tavern,
	};

	static t_town_drawing_order const k_building_order[k_town_type_count] =
	{
		{ k_life_drawing_order,	ELEMENTS_OF( k_life_drawing_order )   },
		{ k_order_drawing_order,	ELEMENTS_OF( k_order_drawing_order )  },
		{ k_death_drawing_order,	ELEMENTS_OF( k_death_drawing_order )  },
		{ k_chaos_drawing_order,	ELEMENTS_OF( k_chaos_drawing_order )  },
		{ k_nature_drawing_order,	ELEMENTS_OF( k_nature_drawing_order ) },
		{ k_might_drawing_order,	ELEMENTS_OF( k_might_drawing_order ) }
	};

	// --------------------------------------------------------
	// buildings which replace older buildings
	// --------------------------------------------------------
	t_town_building k_replacement_list[] =
	{
		k_town_citadel,			k_town_fort,
		k_town_castle,			k_town_citadel,
		k_town_hall,			k_town_village_hall,
		k_town_city_hall,		k_town_hall,
		k_town_mage_guild_2,	k_town_mage_guild_1,
		k_town_mage_guild_3,	k_town_mage_guild_2,
		k_town_mage_guild_4,	k_town_mage_guild_3,
		k_town_mage_guild_5,	k_town_mage_guild_4
	};

	class t_replacement_array
	{
	public:
		t_replacement_array();

		t_town_building_set const& operator[]( t_town_building building ) const;
	protected:
		t_town_building_set m_replacements[k_town_building_count];
	};

	inline t_town_building_set const& t_replacement_array::operator []( t_town_building building ) const
	{
		return m_replacements[building];
	}

};

t_replacement_array::t_replacement_array()
{
	int             i = 0;
	t_town_building building;

	for (i = 0; i < k_town_building_count; i++)
		m_replacements[i].reset();
	i = 0;
	while (i < ELEMENTS_OF( k_replacement_list ))
	{
		building = k_replacement_list[i++];
		if (i >= ELEMENTS_OF( k_replacement_list ))
			break;
		m_replacements[building].set( k_replacement_list[i] );
		m_replacements[building] |= m_replacements[k_replacement_list[i++]];
	}
}

t_town_drawing_order const& get_drawing_order( t_town_type town )
{
	return k_building_order[town];
}


t_town_building_set const& get_replaced_buildings( t_town_building building )
{
	static t_replacement_array replacements;

	return replacements[building];
}
